package com.yc.chaoshi.cs_manage.Interceptor;

import cn.hutool.core.util.StrUtil;
import com.yc.chaoshi.cs_manage.mapper.UmsUserMapper;
import com.yc.chaoshi.cs_manage.service.IUmsUserService;
import com.yc.chaoshi.cs_manage.service.impl.UmsUserServiceImpl;
import com.yc.chaoshi.cs_manage.util.JwtTokenUtil;
import com.yc.chaoshi.entity.UmsResource;
import com.yc.chaoshi.entity.UmsUser;
import com.yc.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

public class LoginInterceptor implements HandlerInterceptor {
    UmsUserServiceImpl service=new UmsUserServiceImpl();

    private JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    /**
     * 目标方法执行前
     * 该方法在控制器处理请求方法前执行，其返回值表示是否中断后续操作
     * 返回 true 表示继续向下执行，返回 false 表示中断后续操作
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(request.getRequestURI());
        String jwt = request.getHeader("authorization");
        if (!StrUtil.isBlank(jwt) && jwt.startsWith("Bearer")) {
           String id = jwt.substring("Bearer".length());
//            String username = jwtTokenUtil.getUserNameFromToken(jwt);
            System.out.println(id);
//            String id = jwtTokenUtil.getUserIdFromToken(jwt);
            if (!StrUtil.isBlank(id)) {
                List<UmsResource> resources = service.ResourceByid(Integer.valueOf(id));
                System.out.println(resources);
                if (resources != null) {
//                    jwtTokenUtil.CurrentUser.set(user);
                    System.out.println(resources);
                }
            }
        }
        return false;
    }
}

